'use strict';

/**
 * @ngdoc service
 * @name jgappApp.UserInfo
 * @description
 * # UserInfo
 * Service in the jgappApp.
 */
angular.module('jgappApp')
.factory('ContactInfo', function($http, openApi, localStorageService, $q, $timeout) {
        // AngularJS will instantiate a singleton by calling "new" on this function
        var ContactInfo = {}
        ContactInfo.getData = function() {
            return  $http
                .get(openApi.addressbookTree)
                .then(function(response, status, headers, config) {
                var data=response.data;
                  if (data.success) {
                    localStorageService.set("ContactInfo", data.result)
                  }
                  return data.result
                });
            } 
        
        ContactInfo.getDatabyUserId = function(userId) {
            var defer = $q.defer();
            ContactInfo.getData().then(function(data) {
                data.forEach(function(item, index) {
                    item.children&&item.children.forEach(function(subitem, index) {
                        if (subitem.id == userId) {
                            defer.resolve(subitem);
                        }
                    });
                });
                defer.resolve(data);
            })
            return defer.promise;
        }
        return ContactInfo
    });
